function MainAssistant(params) {
	/** this is the creator function for your scene assistant object. It will be passed all the 
	*   additional parameters (after the scene name) that were passed to pushScene. The reference
	*   to the scene controller (this.controller) has not be established yet, so any initialization
	*   that needs the scene controller should be done in the setup function below.
	*/
	  this.username = params.username;
	  this.password = params.password;
}

/** this function is for setup tasks that have to happen when the scene is first created 
*/
MainAssistant.prototype.setup = function() {
	this.controller.get('username').update(this.username);
		
	/** use Mojo.View.render to render view templates and add them to the scene, if needed.
	 */
	
	/** setup widgets here-
	 * Instantiates UI widgets for login scene. Each widget requires attributes and a model.
	 * These are passed to the widget in a JSON object.
	 */
	this.controller.setupWidget("starMapButton",
         this.starMapButtonAttributes = {
		 	type: Mojo.Widget.Button
             },
         this.starMapButtonModel = {
             label : "Star Map",
             disabled: false
         });
		 
	this.controller.setupWidget("virtualTelescopeButton",
         this.virtualTelescopeButtonAttributes = {
		 	type: Mojo.Widget.Button
             },
         this.virtualTelescopeButtonModel = {
             label : "Virtual Telescope",
             disabled: false
         });
		 
	this.controller.setupWidget("satelliteTrackingButton",
         this.satelliteTrackingButtonAttributes = {
		 	type: Mojo.Widget.Button
             },
         this.satelliteTrackingButtonModel = {
             label : "Satellite Tracking",
             disabled: false
         });
	this.findMe();
	
	/** add event handlers to listen to events from widgets
	 */
	this.starMapButtonHandler = this.starMapButton.bindAsEventListener(this);
	this.virtualTelescopeButtonHandler = this.virtualTelescopeButton.bindAsEventListener(this);
	this.satelliteTrackingButtonHandler = this.satelliteTrackingButton.bindAsEventListener(this);
}
/**This is the listener function for the buttons on the main page.  When a button
 *is clicked the appropriate sub-routine is called
 *@param event
 **/
MainAssistant.prototype.activate = function(event) {
	/** put in event handlers here that should only be in effect when this scene is active. For
	 * example, key handlers that are observing the document
	 * When the scene is activated, start the event listeners for taps on the 
	 * 'Star Map', 'Virtual Telescope', and 'Satellite Tracking' buttons.
	 */
	  Mojo.Event.listen(this.controller.get('starMapButton'),Mojo.Event.tap,this.starMapButtonHandler);
	  Mojo.Event.listen(this.controller.get('virtualTelescopeButton'),Mojo.Event.tap,this.virtualTelescopeButtonHandler);
	  Mojo.Event.listen(this.controller.get('satelliteTrackingButton'),Mojo.Event.tap,this.satelliteTrackingButtonHandler);
}

/** remove any event handlers you added in activate and do any other cleanup that should happen before
	  * this scene is popped or another scene is pushed on top
	  */
MainAssistant.prototype.deactivate = function(event) {
	
	  Mojo.Event.stopListening(this.controller.get('starMapButton'),Mojo.Event.tap,this.starMapButtonHandler);
	  Mojo.Event.stopListening(this.controller.get('virtualTelescopeButton'),Mojo.Event.tap,this.virtualTelescopeButtonHandler);
	  Mojo.Event.stopListening(this.controller.get('satelliteTrackingButton'),Mojo.Event.tap,this.satelliteTrackingButtonHandler);
}

/** this function should do any cleanup needed before the scene is destroyed as 
	 *  a result of being popped off the scene stack
	 */
MainAssistant.prototype.cleanup = function(event) {
	
}

/** This function is called when the starmap button is pressed. It pushes the starmap scene
 * onto the scene stack.
 */
MainAssistant.prototype.starMapButton = function(event) {
	if (!event)
		return;
	this.controller.stageController.pushScene('starmap', event);
}

/** This function is called when the virtual telescope button is pressed.
 * It pushes the virtual telescope scene onto the scene stack.
 */
MainAssistant.prototype.virtualTelescopeButton = function(event) {
	if (!event)
		return;
	this.controller.stageController.pushScene('virtualtelescope', event);
}

/** This function is called when the satellite tracking button is pressed.
 * It pushes the satellite tracking scene onto the scene stack.
 */
MainAssistant.prototype.satelliteTrackingButton = function(event) {
	if (!event)
		return;
	this.controller.stageController.pushScene('satellitetracking', event);
}

/** This is the callback function for  finding your location.  It call the updateLocation
 *function, or outputs an error message.
 *@param data The location data for the current device.
	 *
	 */
MainAssistant.prototype.locationCallback = function(data) {
	var locationResponse;
	switch (data.errorCode) {
					case 0:
						locationResponse = "Latitude: " + data.latitude + "\n" + "Longitude: " + data.longitude;
						break;
					case 1:
						locationResponse = "GPS Timeout.";
						break;
					case 2:
						locationResponse = "Position Unavailable.";
						break;
					case 5:
						locationResponse = "Location services are turned off.";
						break;					
					default:
						locationResponse = "Location error: " + data.errorCode;
						break;
				}
				if (data.errorCode == 0) {
					this.updateLocation(data);
				}
				else {
					this.controller.showAlertDialog({
				    	title: $L("GPS Error"),
				    	message: $L('Could not save current location to server.'),
						choices:[{label:$L('Ok')}]
					});
				}
}

/** This function actually queries the palm server for current position and then
 * sends that data to locationCallback, a callback function.
 */
MainAssistant.prototype.findMe = function(event) {
	this.controller.serviceRequest('palm://com.palm.location', {
		method:"getCurrentPosition",
		parameters:{accuracy: 'Medium', maximumAge: 10, responseTime: 1},
		onSuccess: function( data ) 
			{
				this.locationCallback(data);
			}.bind(this),
		onFailure: function( data )
			{
				this.locationCallback(data);
			}.bind(this),
		}
	);
}

/** This function recieves location data, and then makes a remote server call
 * given that data to update the devices current location on the server.
 * @param data The current device's location data.
 */
MainAssistant.prototype.updateLocation = function(data) {    
    var url = "http://www.bmcstudiosllc.com/starmap/" + "mobile_update.php";

    try{
        var request = new Ajax.Request(url,
        {
            method:'get',
            parameters: {'username': this.username, 'password': this.password, 'latitude': data.latitude, 'longitude': data.longitude},
            evalJSON:'true',
            onComplete: this.updateUserCallback.bind(this),
        });
    }
    catch (e)
    {
        Mojo.log.error(e);
        return 0;
    }
}

/** Callback function for updateLocation that outputs an error or the recieved data
 * from call to server.
 */
MainAssistant.prototype.updateUserCallback = function(ajaxResponse) {
	if (ajaxResponse.responseJSON.returnCode != 1) {
		Mojo.Controller.errorDialog(ajaxResponse.responseJSON.message);
	}
}
